<?php

class XmlSearcher {

	function GetElements( &$oSearchContainer, $oXmlElement, $aSearchConfig ) {
		if ( !is_object( $oSearchContainer ) ) $oSearchContainer = new XmlElement( 'SearchContainer' );

		$oTempContainer = null;

		$sSearchCommand = array_shift( $aSearchConfig );
		XmlSearcher::GetElementsByName( $oTempContainer, $oXmlElement, $sSearchCommand );
		
		if ( !empty( $aSearchConfig ) )  {
			XmlSearcher::GetElements( $oSearchContainer, $oTempContainer, $aSearchConfig );
		} else {
			$oSearchContainer->MergeElements( $oTempContainer );
		}
	}
	
	function GetElementByName( $oXmlElement, $sElementName ) {
		$oSearchContainer = null;
		XmlSearcher::GetElementsByName( $oSearchContainer, $oXmlElement, $sElementName, false );
		$oSearchContainer->RewindElementIterator();
		return $oSearchContainer->GetNextElement();
	}
	
	function GetElementsByName( &$oSearchContainer, &$oXmlElement, $sElementName, $bRecurse = true ) {

		if ( !is_object( $oSearchContainer ) ) $oSearchContainer = new XmlElement( 'SearchContainer' );
		
		$oXmlElement->RewindElementIterator();
		while( $oXmlElement->HasAnotherElement() ) {
			$oThisElement = $oXmlElement->GetNextElement();
			if ( strtolower( $oThisElement->GetName() ) == strtolower( $sElementName ) ) {
				$oSearchContainer->AddElement( $oThisElement );
			}
			if ( $bRecurse ) {
				XmlSearcher::GetElementsByName( $oSearchContainer, $oThisElement, $sElementName );
			}
		}
	}
	
	function GetElementWithAttribute( &$oXmlElement, $sAttributeName, $sAttributeValue ) {
		
		$oSearchContainer = null;
		XmlSearcher::GetElementsWithAttribute( $oSearchContainer, $oXmlElement, $sAttributeName, $sAttributeValue );
		$oSearchContainer->RewindElementIterator();
		return $oSearchContainer->GetNextElement();
	}
	
	function GetElementsWithAttribute( &$oSearchContainer, &$oXmlElement, $sAttributeName, $sAttributeValue ) {
		
		if ( !is_object( $oSearchContainer ) ) $oSearchContainer = new XmlElement( 'SearchContainer' );

		$oXmlElement->RewindElementIterator();
		while( $oXmlElement->HasAnotherElement() ) {
			$oThisElement = $oXmlElement->GetNextElement();
			if (  strtoupper( $oThisElement->GetAttribute( $sAttributeName ) ) == strtoupper( $sAttributeValue ) ) {
				$oSearchContainer->AddElement( $oThisElement );
			}
		}
	}


}
?>
